package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.ComponentInfoExcel;
import com.ruoyi.system.domain.ConnDrawComponentinfo;
import com.ruoyi.system.service.ComponentExcelService;
import com.ruoyi.system.service.IConnDrawComponentinfoService;
import com.ruoyi.system.utils.ComponentExcelUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class ComponentExcelServiceImpl implements ComponentExcelService {
    private IConnDrawComponentinfoService drawComponentinfoService;

    @Override
    public void exportExcel(HttpServletResponse response,ConnDrawComponentinfo connDrawComponentinfoReqVo) {
        try {
            List<ComponentInfoExcel> dataList = new ArrayList<>();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            Workbook workbook = ComponentExcelUtils.generateWorkBook("test",dataList);
            workbook.write(byteArrayOutputStream);
            workbook.close();

            // http响应信
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("test"+ "工程图例导出.xlsx", "UTF-8"));

            ServletOutputStream outputStream = response.getOutputStream();
            byteArrayOutputStream.writeTo(outputStream);
            outputStream.flush();
            outputStream.close();
            byteArrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
